﻿Imports MySql.Data.MySqlClient
Imports System.IO
Imports System.Net
Imports System.Xml
Imports DevExpress.XtraGrid.Views.Grid
Public Class frmPPPerson
    Dim DA As New MySqlDataAdapter
    Dim MyConn As New MySqlConnection
    Dim mysql As New ClsSQLhelper
    Dim DSOPPP As New OPPP
    Dim Strsql As String
    Dim Curr_View As String = "Default"
    Dim On_load As Boolean = True
    Dim StrMainSql As String = "Select m_person.PCUCODE,m_person.PID,m_person.HN,m_person.CID,Concat(m_person.NAME,'  ',m_person.LNAME) as NAME,m_person.SEX,m_person.BIRTH,m_person.MSTATUS,m_person.OCCUPA,m_person.NATION,m_person.TYPEAREA,m_person.CODE_ID from m_person where status = 1 "
#Region "Function"
    Sub Load_Data(ByVal Code_ID As String, Optional ByVal StrAddonSql As String = "")
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Curr_View = Code_ID
            'DSOPPP.Tables("m_person").Clear()
            'Strsql = "Select m_person.PCUCODE,m_person.PID,m_person.HN,m_person.CID,Concat(m_person.NAME,'  ',m_person.LNAME) as NAME,m_person.SEX,m_person.BIRTH,m_person.MSTATUS,m_person.OCCUPA,m_person.NATION,m_person.TYPEAREA,m_person.CODE_ID from m_person where status = 1 and code_id Like '%OP9200%' and FILENAME = " & Me.CboImportOPFile.SelectedValue
            '  Strsql = "select m_service.PCUCODE,m_service.PID,m_person.CID,m_person.HN,Concat(m_person.NAME,'  ',m_person.LNAME) as NAME,m_person.SEX,m_person.BIRTH,m_service.SEQ,m_service.DATE_SERV,m_service.CLINIC,m_service.PRICE,m_service.PAY,m_service.SERVICE_TYPE,m_service.REFERIN,m_service.REFINHOS,m_service.REFEROUT,m_service.REFOUHOS  from m_service left join m_person on  m_service.PID = m_person.PID  where m_service.CODE_ID  like  '%OP9200%' and m_service.FILENAME = " & Me.CboImportOPFile.SelectedValue & "   order by  m_service.PID"
            Strsql = "Select m_person.PCUCODE,m_person.PID,m_person.HN,m_person.CID,Concat(m_person.NAME,'  ',m_person.LNAME) as NAME,m_person.SEX,m_person.BIRTH,m_person.MSTATUS,m_person.OCCUPA,m_person.NATION,m_person.TYPEAREA,m_person.CODE_ID from m_person where status = 1 and "
            Strsql = StrMainSql & " and code_id Like '%" & Code_ID & "%'  " & StrAddonSql & " and FILENAME = " & Me.CboImportOPFile.SelectedValue & " order by cid,hn"

            '  DA = New MySqlDataAdapter(Strsql, MyConn)
            ' DA.Fill(DSOPPP, "m_person")
            Dim DT_Person As New DataTable
            DT_Person = mysql.GetMYSQLDataTable(Strsql, "m_PERSON")
            With DataGridView1
                .RowHeadersVisible = False
                .AllowDrop = False
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .AllowUserToOrderColumns = True
                .AllowUserToResizeColumns = True
                .AllowUserToResizeRows = False
                .MultiSelect = False
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                .DataSource = DT_Person

                If Me.DataGridView1.RowCount < 1 Then
                    MsgBox("ไม่พบข้อมูล", MsgBoxStyle.Exclamation)
                Else
                    MsgBox("ดึงข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
                End If
            End With
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub GenScript()
        Try
            Dim Strsql As String = " CID in ("
            Dim HNSql As String = " HN in ("
            Dim PIDsql As String = " person_id in ("

            Select Case Me.Curr_View
                Case "CheckNullHN"


                    Strsql = "Insert into ptcardno (hn,cardtype,cardno) select hn,'01' as cardtype,concat(left(cid,1),'-',mid(cid,2,4),'-',mid(cid,6,5),'-',mid(cid,11,2),'-',right(cid,1)) as cardno from patient where cid in ("
                    For i As Integer = 0 To DataGridView1.RowCount - 1

                        Strsql = Strsql & "'" & DataGridView1.Rows(i).Cells(3).Value.ToString.Trim() & "',"

                    Next
                    Strsql = Mid(Strsql, 1, Len(Strsql) - 1) & ")"
                    SQLDialog.Sqltext.Text = Strsql
                    SQLDialog.ShowDialog()
                Case "PE9200"


                    ' Strsql = "Insert into ptcardno (hn,cardtype,cardno) select hn,'01' as cardtype,concat(left(cid,1),'-',mid(cid,2,4),'-',mid(cid,5,5),'-',mid(cid,11,2),'-',right(cid,1)) as cardno from patient where cid in ("
                    For i As Integer = 0 To DataGridView1.RowCount - 1

                        Strsql = Strsql & "'" & DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() & "',"
                        HNSql = HNSql & "'" & DataGridView1.Rows(i).Cells("HN").Value.ToString.Trim() & "',"
                        PIDsql = PIDsql & "'" & DataGridView1.Rows(i).Cells("PID").Value.ToString.Trim() & "',"
                    Next
                    Strsql = Mid(Strsql, 1, Len(Strsql) - 1) & ")"
                    HNSql = Mid(HNSql, 1, Len(HNSql) - 1) & ")"
                    PIDsql = Mid(PIDsql, 1, Len(PIDsql) - 1) & ")"
                    SQLDialog.Sqltext.Text = Strsql
                    SQLDialog.Sqltext2.Text = HNSql
                    SQLDialog.Sqltext3.Text = PIDsql

                    SQLDialog.ShowDialog()
                Case "PE9202"
                    ' Strsql = "Insert into ptcardno (hn,cardtype,cardno) select hn,'01' as cardtype,concat(left(cid,1),'-',mid(cid,2,4),'-',mid(cid,5,5),'-',mid(cid,11,2),'-',right(cid,1)) as cardno from patient where cid in ("
                    For i As Integer = 0 To DataGridView1.RowCount - 1

                        Strsql = Strsql & "'" & DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() & "',"
                        HNSql = HNSql & "'" & DataGridView1.Rows(i).Cells("HN").Value.ToString.Trim() & "',"
                        PIDsql = PIDsql & "'" & DataGridView1.Rows(i).Cells("PID").Value.ToString.Trim() & "',"
                    Next
                    Strsql = Mid(Strsql, 1, Len(Strsql) - 1) & ")"
                    HNSql = Mid(HNSql, 1, Len(HNSql) - 1) & ")"
                    PIDsql = Mid(PIDsql, 1, Len(PIDsql) - 1) & ")"
                    SQLDialog.Sqltext.Text = Strsql
                    SQLDialog.Sqltext2.Text = HNSql
                    SQLDialog.Sqltext3.Text = PIDsql

                    SQLDialog.ShowDialog()
                Case "PE9203"
                    ' Strsql = "Insert into ptcardno (hn,cardtype,cardno) select hn,'01' as cardtype,concat(left(cid,1),'-',mid(cid,2,4),'-',mid(cid,5,5),'-',mid(cid,11,2),'-',right(cid,1)) as cardno from patient where cid in ("
                    For i As Integer = 0 To DataGridView1.RowCount - 1

                        Strsql = Strsql & "'" & DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() & "',"
                        HNSql = HNSql & "'" & DataGridView1.Rows(i).Cells("HN").Value.ToString.Trim() & "',"
                        PIDsql = PIDsql & "'" & DataGridView1.Rows(i).Cells("PID").Value.ToString.Trim() & "',"
                    Next
                    Strsql = Mid(Strsql, 1, Len(Strsql) - 1) & ")"
                    HNSql = Mid(HNSql, 1, Len(HNSql) - 1) & ")"
                    PIDsql = Mid(PIDsql, 1, Len(PIDsql) - 1) & ")"
                    SQLDialog.Sqltext.Text = Strsql
                    SQLDialog.Sqltext2.Text = HNSql
                    SQLDialog.Sqltext3.Text = PIDsql

                    SQLDialog.ShowDialog()
                Case "PE1111"
                    For i As Integer = 0 To DataGridView1.RowCount - 1

                        Strsql = Strsql & "'" & DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() & "',"
                        HNSql = HNSql & "'" & DataGridView1.Rows(i).Cells("HN").Value.ToString.Trim() & "',"
                        PIDsql = PIDsql & "'" & DataGridView1.Rows(i).Cells("PID").Value.ToString.Trim() & "',"
                    Next
                    Strsql = Mid(Strsql, 1, Len(Strsql) - 1) & ")"
                    HNSql = Mid(HNSql, 1, Len(HNSql) - 1) & ")"
                    PIDsql = Mid(PIDsql, 1, Len(PIDsql) - 1) & ")"
                    SQLDialog.Sqltext.Text = Strsql
                    SQLDialog.Sqltext2.Text = HNSql
                    SQLDialog.Sqltext3.Text = PIDsql

                    SQLDialog.ShowDialog()
                Case "PE1102"
                    Strsql = " CID in ("
                    For i As Integer = 0 To DataGridView1.RowCount - 1

                        Strsql = Strsql & "'" & DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() & "',"
                        HNSql = HNSql & "'" & DataGridView1.Rows(i).Cells("HN").Value.ToString.Trim() & "',"
                        PIDsql = PIDsql & "'" & DataGridView1.Rows(i).Cells("PID").Value.ToString.Trim() & "',"
                    Next
                    Strsql = Mid(Strsql, 1, Len(Strsql) - 1) & ")"
                    HNSql = Mid(HNSql, 1, Len(HNSql) - 1) & ")"
                    PIDsql = Mid(PIDsql, 1, Len(PIDsql) - 1) & ")"
                    SQLDialog.Sqltext.Text = Strsql
                    SQLDialog.Sqltext2.Text = HNSql
                    SQLDialog.Sqltext3.Text = PIDsql

                    SQLDialog.ShowDialog()
                Case "Default"

            End Select

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub Search_Information()
        Try
            If Me.VGR_PID.Properties.Value = "000000" Then
                MsgBox("ไม่สามารถหาความเชื่อมโยงได้เนื่องจาก PID = 000000")
                Exit Sub
            End If
            Select Case Me.VGR_CID.Properties.Value
                Case "0000000000000"
                    If MsgBox("รหัสบัตรไม่ถูกกต้อง คุณยืนยันที่จะค้นหาต่อหรือไม่", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "คำเตือน") = MsgBoxResult.No Then Exit Sub
                    frmSearchInfo.TxtCriteria.Text = Me.VGR_PID.Properties.Value
                Case ""
                    If Me.VGR_HN.Properties.Value <> "ไม่ระบุ" Then

                        frmSearchInfo.TxtCriteria.Text = Me.VGR_HN.Properties.Value
                    Else
                        If Me.VGR_PID.Properties.Value <> "" Then
                            frmSearchInfo.TxtCriteria.Text = Me.VGR_PID.Properties.Value
                        ElseIf Me.VGR_Fullname.Properties.Value <> "" Then
                            frmSearchInfo.TxtCriteria.Text = Me.VGR_Fullname.Properties.Value
                        Else

                            MsgBox("ไม่สามารถ หาความเชื่อมโยงได้", MsgBoxStyle.Critical)
                        End If

                    End If
                Case Else
                    frmSearchInfo.TxtCriteria.Text = Me.VGR_CID.Properties.Value
            End Select
            With frmSearchInfo
                .TxtYear.Text = Me.TxtYear.Text
                .TxtMonth.Text = Me.TxtMonth.Text
                '.TxtDateServ.Text = Me.TxtDateServ.Text
                .Submit_Search()
                .ShowDialog()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    'Function Read_DataQuality()
    '    Try
    '        'Read total record in table m_person
    '        Strsql = "Select count(*) as totoalRecords from m_person  where  FILENAME = " & Me.CboImportOPFile.SelectedValue
    '        LVScore.Items(0).SubItems.Item(1).Text = String.Format(mysql.MySQLExecuteScalar(Strsql), "#,##0")

    '        'Read Duplicat CID in table m_person
    '        Dim DT_DupCID As New DataTable
    '        Strsql = "Select * from m_person where status = 1 and code_id Like '%PE9202%'  and FILENAME = " & Me.CboImportOPFile.SelectedValue
    '        DA = New MySqlDataAdapter(Strsql, MyConn)
    '        DA.Fill(DSOPPP, "m_duplicateCID")
    '        LVScore.Items(1).SubItems.Item(1).Text = DSOPPP.Tables("m_duplicateCID").Rows.Count

    '        'Read Wrong Format CID in table m_person
    '        Dim DT_WrongFormatCID As New DataTable
    '        Strsql = "select CID,count(CID) from m_person where  FILENAME = " & Me.CboImportOPFile.SelectedValue & "  group by CID having Length(CID)<>13   order by count(CID) desc"
    '        DA = New MySqlDataAdapter(Strsql, MyConn)
    '        DA.Fill(DSOPPP, "m_wrongformatCID")
    '        LVScore.Items(2).SubItems.Item(1).Text = DSOPPP.Tables("m_wrongformatCID").Rows.Count

    '        'Read Non Relationship
    '        Dim DT_unRelation As New DataTable
    '        Strsql = "Select m_person.PCUCODE,m_person.PID,m_person.HN,m_person.CID,Concat(m_person.NAME,'  ',m_person.LNAME) as NAME,m_person.SEX,m_person.BIRTH,m_person.MSTATUS,m_person.OCCUPA,m_person.NATION,m_person.TYPEAREA,m_person.CODE_ID from m_person where status = 1 and code_id Like '%PE9200%' and FILENAME = " & Me.CboImportOPFile.SelectedValue
    '        'DT_unRelation = mysql.GetMYSQLDataTable(Strsql, "m_person", True)
    '        DA = New MySqlDataAdapter(Strsql, MyConn)
    '        DA.Fill(DSOPPP, "m_NonRelationperson")
    '        LVScore.Items(3).SubItems.Item(1).Text = DSOPPP.Tables("m_NonRelationperson").Rows.Count


    '        DT_DupCID.Dispose()
    '        DT_WrongFormatCID.Dispose()

    '    Catch ex As Exception
    '        MsgBox(ex.Message)
    '    End Try
    'End Function
    Sub Load_Summary()
        Try
            Strsql = "select CODE_ID,Count(CODE_ID) as Totalrows from m_person where filename = " & CboImportOPFile.SelectedValue & " and CODE_ID is not null  group by CODE_ID "
            Dim DT_Service As New DataTable
            DT_Service = mysql.GetMYSQLDataTable(Strsql, "ERROR_Summary")

            With Grid_ErrorCode
                .DataSource = DT_Service

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
#End Region






    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub



    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Try
            Dim Error_Code As String

            With Me.DataGridView1
                Dim MyPerson As New ClsPerson
                MyPerson.Get_NHSO_Profile(.Rows(e.RowIndex).Cells("PID").Value.ToString.Trim)
                Me.VGR_CID.Properties.Value = "0000000000000"
                Me.VGR_CID.Properties.Value = MyPerson.CID
                Me.VGR_PID.Properties.Value = .Rows(e.RowIndex).Cells("PID").Value.ToString.Trim
                Me.VGR_Fullname.Properties.Value = MyPerson.Get_Person_FullName
                Me.VGR_HN.Properties.Value = MyPerson.HN
                Me.VGR_MStatus.Properties.Value = MyPerson.MStatus
                Me.VGR_FormatCID.Properties.Value = MyPerson.Get_Person_FormatCID
                Me.VGR_Nationality.Properties.Value = MyPerson.Nation
                Me.VGR_Occupation.Properties.Value = MyPerson.Ocupaton
                Me.VGR_Race.Properties.Value = MyPerson.Race
                Me.VGR_Sex.Properties.Value = MyPerson.Sex
                Me.VGR_TypeArea.Properties.Value = MyPerson.TypeArea
                Me.VGA_BirthDay.Properties.Value = MyPerson.BirthDay
                Me.VGA_AGE.Properties.Value = MyPerson.Age



                Error_Code = DataGridView1.Rows(e.RowIndex).Cells("CODE_ID").Value

                If Mid(Error_Code, 1, 1) = "," Then
                    Error_Code = Mid(Error_Code, 2, Error_Code.Length)
                End If
                If InStr(Error_Code, ",", CompareMethod.Text) > 0 Then
                    Error_Code = Replace(Error_Code, ",", "','")
                End If
                Strsql = "select code_id,remark from l_error_code where code_id in ('" & Error_Code & "')"
                Dim DT_ERROR As New DataTable
                DT_ERROR = mysql.GetMYSQLDataTable(Strsql, "l_error_code")
                With ErrorDesc_Grid
                    .DataSource = DT_ERROR
                End With

                Strsql = "Select Code,DESC_R,SEX,AGEDUSE,AGEMIN,AGEMAX from l_icd10 where code =  '" & DataGridView1.Rows(e.RowIndex).Cells("DIAGCODE").Value & "'"
                Dim DTICDWHO As New DataTable
                DTICDWHO = mysql.GetMYSQLDataTable(Strsql, "l_icd10")



            End With
        Catch ex As Exception

        End Try

    End Sub
    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

        Search_Information()
    End Sub

    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.F5 Then
            Search_Information()
        End If
    End Sub




    Private Sub CboImportOPFile_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboImportOPFile.SelectedIndexChanged
        Try
            If On_load = True Then Exit Sub
            If InStr(Me.CboImportOPFile.Text, "FOP", CompareMethod.Text) > 0 Then
                Me.TxtYear.Text = Mid(Me.CboImportOPFile.Text, 11, 4)
                Me.TxtMonth.Text = Mid(Me.CboImportOPFile.Text, 15, 2)
            End If
            If InStr(Me.CboImportOPFile.Text, "E_PCU", CompareMethod.Text) > 0 Then
                'E_PCU_11266_201010_20101202.zip
                Me.TxtYear.Text = Val(Mid(Me.CboImportOPFile.Text, 13, 4)) + 543
                Me.TxtMonth.Text = Mid(Me.CboImportOPFile.Text, 17, 2)
            End If

            'Read_DataQuality()
            Load_Summary()

            'Select Case Me.Curr_View
            '    Case "PE9200"
            '        BtnCheckNoHN_Click(sender, e)
            '    Case "PE9202"
            '        BtnCheckDupCID_Click(sender, e)
            '    Case "PE9203"
            '        BtnCheckDupPID_Click(sender, e)
            '    Case "PE1111"
            '        BtnCheckOccupation_Click(sender, e)
            '    Case "PE1102"
            '        BtnCheckCID_Click(sender, e)
            '    Case "CheckNullHN"
            '        BtnCheckHN_Click(sender, e)
            'End Select
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
    Private Sub RBM_PE1102_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE1102.ItemClick
        Load_Data("PE1102")

    End Sub

    Private Sub RBM_PE1111_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE1111.ItemClick
        Load_Data("PE1111")
    End Sub

    Private Sub RBM_PE9203_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE9203.ItemClick
        Load_Data("PE9203")
    End Sub


    Private Sub RBM_PE9202_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE9202.ItemClick
        Me.mnCopy.Items(1).Enabled = True
        Load_Data("PE9202")
    End Sub

    Private Sub RBM_PE9200_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE9200.ItemClick
        Load_Data("PE9200")
    End Sub

    Private Sub RBM_PE_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE.ItemClick
        ' Load_Data()

        Try

            Me.Cursor = Cursors.WaitCursor
            Me.Curr_View = "CheckNullHN"
            DSOPPP.Tables("m_person").Clear()
            Strsql = "Select m_person.PCUCODE,m_person.PID,m_person.HN,m_person.CID,Concat(m_person.NAME,'  ',m_person.LNAME) as NAME,m_person.SEX,m_person.BIRTH,m_person.MSTATUS,m_person.OCCUPA,m_person.NATION,m_person.TYPEAREA,m_person.CODE_ID from m_person where status = 1 and hn = ''  and FILENAME = " & Me.CboImportOPFile.SelectedValue
            ' DA = New MySqlDataAdapter(Strsql, MyConn)
            ' DA.Fill(DSOPPP, "m_person")
            Dim DT_Person As New DataTable
            DT_Person = mysql.GetMYSQLDataTable(Strsql, "m_person")
            With DataGridView1
                .RowHeadersVisible = False
                .AllowDrop = False
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .AllowUserToOrderColumns = True
                .AllowUserToResizeColumns = True
                .AllowUserToResizeRows = False
                .MultiSelect = False
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                DataGridView1.DataSource = DT_Person ' DSOPPP.Tables("m_person")
                ' lblDescription.Text = "กรุณาตรวจสอบ Table ptcardno ว่ามี CID เหล่านี้อยู่หรือไม่"

                If Me.DataGridView1.RowCount < 1 Then
                    MsgBox("ไม่พบข้อมูล", MsgBoxStyle.Exclamation)
                Else
                    MsgBox("ดึงข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
                End If


            End With
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub DockPanel2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DockPanel2.Click

    End Sub

    Private Sub RBM_PE1107_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE1107.ItemClick
        Load_Data("PE1107")
    End Sub

    Private Sub RBM_PE1109_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE1109.ItemClick
        Load_Data("PE1109")
    End Sub

    Private Sub RBM_PE1110_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE1110.ItemClick
        Load_Data("PE1110")
    End Sub

    Private Sub RBM_PE1112_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE1112.ItemClick
        Load_Data("PE1112")
    End Sub

    Private Sub RBM_PE1140_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_PE1140.ItemClick
        Load_Data("PE1140")
    End Sub

    Private Sub RBM_Find_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_Find.ItemClick
        Search_Information()
    End Sub

    Private Sub RBM_GenScript_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_GenScript.ItemClick
        GenScript()
    End Sub

    Private Sub RBM_ExcelExport_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles RBM_ExcelExport.ItemClick
        If Me.DataGridView1.RowCount = 0 Then Exit Sub


        If Me.DataGridView1.RowCount = 0 Then Exit Sub
        If MsgBox("คุณต้องการส่งออกข้อมูลในตาราง ออกเป็นรูปแบบ Excel หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Export to Excel") = MsgBoxResult.No Then Exit Sub


        With SaveFileDialog1
            .FileName = Me.Text
            .Filter = "Excel 2003 | *.xls"
            If .ShowDialog() <> DialogResult.OK Then Exit Sub

            Dim Str As String = "" ' = "PCUCODE,PID,HN,CID,NAME,SEX,BIRTH,MSTATUS,OCCUPA,NATION,TYPEAREA,CODE_ID"
            For i = 0 To DataGridView1.ColumnCount - 1
                Str = Str & DataGridView1.Columns(i).HeaderText
                If i <> DataGridView1.ColumnCount - 1 Then
                    Str = Str & ","
                End If

            Next
            Using sw As StreamWriter = New StreamWriter(.FileName, False, System.Text.Encoding.UTF8)
                sw.WriteLine(Str)

                '  
                For i As Integer = 0 To DataGridView1.RowCount - 1
                    Str = ""
                    For j As Integer = 0 To DataGridView1.ColumnCount - 1
                        Str = Str & DataGridView1.Rows(i).Cells(j).Value.ToString
                        If j <> DataGridView1.ColumnCount - 1 Then
                            Str = Str & ","
                        End If

                    Next
                    sw.WriteLine(Str)
                Next
                sw.Close()

            End Using


            MsgBox("ส่งออกข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)

        End With

    End Sub


    Private Sub frmPPPerson_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.mnCopy.Items(1).Enabled = False
            ' Me.TxtZoomCID.Text = "0000000000000"

            Dim DT_m_files_upload As DataTable
            Strsql = "select ROWID,FILENAME from m_files_upload "
            Strsql = Strsql & " where TYPE_DESC = 'PP Package' or  TYPE_DESC = '18 แฟ้ม' "
            Strsql = Strsql & " order by Date_SEND DESC"


            With Me.CboImportOPFile

                DT_m_files_upload = mysql.GetMYSQLDataTable(Strsql, "m_files_upload")
                If DT_m_files_upload.Rows.Count = 0 Then
                    MsgBox("คุณต้องนำข้อมูลเข้าโปรแกรมตรวจสอบ NHSO OPPP 2554 ก่อน", MsgBoxStyle.Exclamation)
                    Exit Sub
                End If
                .DataSource = DT_m_files_upload
                .DisplayMember = "FILENAME"
                .ValueMember = "ROWID"
                Me.CboImportOPFile.SelectionStart = DT_m_files_upload.Rows(0).Item("ROWID").ToString

            End With

            If InStr(Me.CboImportOPFile.Text, "FPP", CompareMethod.Text) > 0 Then
                Me.TxtYear.Text = Mid(Me.CboImportOPFile.Text, 11, 4)
                Me.TxtMonth.Text = Mid(Me.CboImportOPFile.Text, 15, 2)
            End If
            If InStr(Me.CboImportOPFile.Text, "E_PCU", CompareMethod.Text) > 0 Then
                'E_PCU_11266_201010_20101202.zip
                Me.TxtYear.Text = Val(Mid(Me.CboImportOPFile.Text, 13, 4)) + 543
                Me.TxtMonth.Text = Mid(Me.CboImportOPFile.Text, 17, 2)
            End If
            Load_Summary()
            Strsql = "Select (((AMOUNT-ERROR)/amount))*100 as point from m_upload where filename = 'PERSON' and TYPE_DESC = 'PP Package'"
            Dim MyPoint As String
            MyPoint = mysql.MySQLExecuteScalar(Strsql)
            Me.CircularGauge1.Scales(0).Value = Val(MyPoint)
            Me.CircularGauge1.Labels(0).Text = FormatNumber(MyPoint, 2) & " %"
            On_load = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        MyConn.ConnectionString = ClsSQLhelper.MySQLConnectionStr
        ' Read_DataQuality()
    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        If Me.DataGridView1.RowCount = 0 Then Exit Sub
        Me.Cursor = Cursors.WaitCursor

        With frmRptDupCID
            Dim CID_Criteria As String
            If DataGridView1.RowCount = 0 Then Exit Sub
            For i As Integer = 0 To DataGridView1.RowCount - 1

                If DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() <> "" And DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() <> "0000000000000" Then
                    CID_Criteria = CID_Criteria & "'" & DataGridView1.Rows(i).Cells("CID").Value.ToString.Trim() & "',"
                End If
            Next
            CID_Criteria = Mid(CID_Criteria, 1, Len(CID_Criteria) - 1) & ")"
            .CID_Criteria = CID_Criteria
            Me.Cursor = Cursors.Default
            .ShowDialog()
        End With
    End Sub

    Private Sub ViewDetailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewDetailToolStripMenuItem.Click
        Search_Information()
    End Sub

    Private Sub Grid_ErrorCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Grid_ErrorCode.Click

    End Sub

    Private Sub GridView2_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView2.CustomRowCellEditForEditing
        Try
            'If e.Column.FieldName = "Totalrows" Then Return
            Dim Gv As GridView = sender
            Dim CodeName As String = Gv.GetRowCellValue(e.RowHandle, _
              Gv.Columns("CODE_ID")).ToString()
            Load_Data(CodeName)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub DataGridView1_CellClick1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Try
            Dim Error_Code As String

            With Me.DataGridView1
                Dim MyPerson As New ClsPerson
                MyPerson.Get_NHSO_Profile(.Rows(e.RowIndex).Cells("PID").Value.ToString.Trim)
                Me.VGR_CID.Properties.Value = "0000000000000"
                Me.VGR_CID.Properties.Value = MyPerson.CID
                Me.VGR_PID.Properties.Value = .Rows(e.RowIndex).Cells("PID").Value.ToString.Trim
                Me.VGR_Fullname.Properties.Value = MyPerson.Get_Person_FullName
                Me.VGR_HN.Properties.Value = MyPerson.HN
                Me.VGR_MStatus.Properties.Value = MyPerson.MStatus
                Me.VGR_FormatCID.Properties.Value = MyPerson.Get_Person_FormatCID
                Me.VGR_Nationality.Properties.Value = MyPerson.Nation
                Me.VGR_Occupation.Properties.Value = MyPerson.Ocupaton
                Me.VGR_Race.Properties.Value = MyPerson.Race
                Me.VGR_Sex.Properties.Value = MyPerson.Sex
                Me.VGR_TypeArea.Properties.Value = MyPerson.TypeArea
                Me.VGA_BirthDay.Properties.Value = MyPerson.BirthDay
                Me.VGA_AGE.Properties.Value = MyPerson.Age



                Error_Code = DataGridView1.Rows(e.RowIndex).Cells("CODE_ID").Value

                If Mid(Error_Code, 1, 1) = "," Then
                    Error_Code = Mid(Error_Code, 2, Error_Code.Length)
                End If
                If InStr(Error_Code, ",", CompareMethod.Text) > 0 Then
                    Error_Code = Replace(Error_Code, ",", "','")
                End If
                Strsql = "select code_id,remark from l_error_code where code_id in ('" & Error_Code & "')"
                Dim DT_ERROR As New DataTable
                DT_ERROR = mysql.GetMYSQLDataTable(Strsql, "l_error_code")
                With ErrorDesc_Grid
                    .DataSource = DT_ERROR
                End With

                Strsql = "Select Code,DESC_R,SEX,AGEDUSE,AGEMIN,AGEMAX from l_icd10 where code =  '" & DataGridView1.Rows(e.RowIndex).Cells("DIAGCODE").Value & "'"
                Dim DTICDWHO As New DataTable
                DTICDWHO = mysql.GetMYSQLDataTable(Strsql, "l_icd10")



            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub DataGridView1_CellDoubleClick1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Search_Information()
    End Sub
End Class